<?php

namespace Wulian\Controller;

use Common\Controller\HomebaseController;

class DownloadController extends HomebaseController {

    public $m_download,$m_card,$m_user,$m_card_activate,$m_card_import,$m_package,$m_card_status,$m_flow_record,$m_order;
    public $all_type = array(
        "flow_member",   //流量池成员列表
         "card_status"   //流量池成员列表
    );
    protected $quota_type_list = array(
        1=>"流量",
        2=>"时长",
        3=>"条数",
    );
    public $sleep_time = 50;    //毫秒
    public $api_fail = "部分数据返回失败，详情请查看excel";
    function _initialize() {
        parent::_initialize();
        $this->m_download = M("WlDownload");
        $this->m_card= M("WlCard");
        $this->m_user = M("Users");
        $this->m_card_activate = M("WlCardActivate");
        $this->m_card_import = M("WlCardImport");
        $this->m_package = M("WlPackage");
        $this->m_card_status = M("WlCardStatus");
        $this->m_flow_record = M("WlFlowRecord");
        $this->m_order = M("WlOrder");
    }



    /**
     * 下载当前页
     */
    function current(){
        $type = I("get.type");
        $content = read_file($type);
        $export_title = $content['export_title'];
        $export_data = $content['export_data'];

        $excel = new \Common\Lib\excel();
        $excel->export_data($content['excel_title'],$export_title,$export_data);exit;
    }

    /**
     * 发布全部下载任务
     */
    function all(){
        $type = I("get.type");
        $s_start_time = I('get.s_start_time');
        $s_end_time = I('get.s_end_time');
        $s_date = I('get.s_date');
        $s_number = I("get.s_number");
        $s_uid = I("get.s_uid");
//        if(!in_array($type,$this->all_type)){
//            $this->error("参数有误!");exit;
//        }
        $insert = array(
            "download_type"=>$type,
        );
        $download = $this->m_download->where($insert)->find();
        if($download && time()-$download["create_time"]<300){
            echo "<script>alert('5分钟之内不能重复下载!');location.href=history.go(-1);</script>";exit;
        }
        if(!empty($s_start_time)){
            $start_arr = explode('-',$s_start_time);
            if(count($start_arr) == 2){
                $insert["download_starttime"] = strtotime($s_start_time."-01 00:00:00");
            }else{
                $insert["download_starttime"] = strtotime($s_start_time." 00:00:00");
            }

        }

        if(!empty($s_end_time)){
            $insert["download_endtime"] = strtotime($s_end_time." 23:59:59");
        }

        if(!empty($s_date)){
            $insert["download_date_type"] = $s_date;
        }
        if(!empty($s_number)){
            $insert["download_cardlist"] = $s_number;
        }

        if(!empty($s_uid)){
            $insert["download_uid"] = $s_uid;
        }
        $insert["create_time"] = time();
        $insert["proportion"] = session("proportion");
        $result = $this->m_download->add($insert);
        if($result){
            $url = "/m2mplatform/index.php?g=Wulian&m=AdminDownload&a=index";
            echo "<script>alert('任务发布成功');location.href='{$url}';</script>";exit;
        }else{
            echo "<script>alert('请稍后再试');location.href=history.go(-1);</script>";exit;
        }
    }

    /**
     * 全部下载 保存excel文件
     */
    function all_save_excel($content){
        $export_title = $content['export_title'];
        $export_data = $content['export_data'];
        $excel = new \Common\Lib\excel();
        $excel->export_data($content['excel_title'],$export_title,$export_data,1);
    }
    /**
     * 全部下载 流量池成员
     */
    function all_flow_member($download,$file_name){
        global $download_remark;
        $s_number = $download["download_cardlist"];
        $where = array();
        if(!empty($s_number)){
            $where = array("card_iccid"=>$s_number);
        }

        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];
        $card_list = $this->m_card->where($where)->select();
        foreach($card_list AS $citem){
            sleep($this->sleep_time);
            $member = $telecom->getPoolMember($citem['card_number']);
            if(isset($member['OutProdInfos'])){
                $member["OutProdInfos"]["card_iccid"] = $citem["card_iccid"];
                $list[] = $member['OutProdInfos'];
            }else{
                $temp = array();
                $temp["card_iccid"] = $citem["card_iccid"];
                $temp["acc_nbr"]  = "运营商接口请求失败";
                $temp["return_status"] = -1;
                $list[] = $temp;
                $download_remark = $this->api_fail;
                continue;
            }
        }
        foreach ($list AS &$item) {
            if(!isset($item["return_status"])){
                $item["quota_type"] = $this->quota_type_list[$item['quota_type']];
                $card = $this->m_card->where(array("card_number"=>$item["acc_nbr"]))->find();
                if($download["proportion"] == 1){   //实际版本
                    $item["package_data_month"] = ($card["package_data_month"]/$this->wl_proportion)."M";
                    $item["package_data_add"] = ($card["package_data_add"]/$this->wl_proportion)."M";
                    $item["flow_quota"] = $item["package_data_month"];
                    if(!empty($card["package_data_add"])){
                        $item["flow_quota"] = $item["package_data_month"]."+".$item["package_data_add"];
                    }
                }else{  //代理商版本
                    $item["package_data_month"] = ($card["package_data_month"])."M";
                    $item["package_data_add"] = ($card["package_data_add"])."M";
                    $item["flow_quota"] = $item["package_data_month"];
                    if(!empty($card["package_data_add"])){
                        $item["flow_quota"] = $item["package_data_month"]."+".$item["package_data_add"];
                    }
                }
            }

        }

        if(isset($list) && !empty($list)){
            //设置导出数据
            $export_title = array(
                "card_iccid"=>"ICCID",
                "acc_nbr"=>"成员号码",
                "flow_quota"=>"流量限额",
                "quota_type"=>"限额类型",
                "org_id"=>"归属地",
                "eff_date"=>"生效时间",
                "state"=>"状态",
            );
            $export_list = array(
                "excel_title"=>$file_name,
                "export_title"=>$export_title,
                "export_data"=>$list,
            );
            $this->all_save_excel($export_list);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 卡状态查询 全部下载
     */
    function all_card_status($download,$file_name){
        global $download_remark;
        $list = array();
        $s_number = $download["download_cardlist"];
        $number_list = explode(",", $s_number);
        $where = array();
        if(!empty($number_list) && !empty($s_number)){
            $where["card_iccid"] = array("IN",$number_list);
        }

        if(!empty($where)){
            $card_list = $this->m_card->where($where)->select();
        }else{
            $card_list = $this->m_card->select();
        }

        $telecom = new \Wulian\Lib\telecom();
        foreach($card_list AS $nitem){
            sleep($this->sleep_time);
            $temp = array();
            $data = $telecom->getQueryCardStatus($nitem['card_number']);
            $temp["card_iccid"] = $nitem["card_iccid"];
            if(isset($data['Query_response']['prodRecords']['prodRecord']['productInfo']['productStatusName'])){
                $temp['card_number'] = $data['Query_response']['number'];
                $temp['status_name'] = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['productStatusName'];
                if(!is_array($data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'])){
                    $create_date = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'];
                    $create_date = substr($create_date,0,4)."-".substr($create_date,4,2)."-".substr($create_date,6,2);
                    $temp['create_date'] = $create_date;
                }else{
                    $temp['create_date'] = "--";
                }
                $list[] = $temp;
            }else{
                $temp = array();
                $temp["card_iccid"] = $nitem["card_iccid"];
                $temp["card_number"] = "运营商接口请求失败";
                $temp["create_date"] = "";
                $temp["status_name"] = "";
                $list[] = $temp;
                $download_remark = $this->api_fail;
                continue;
            }

        }

        if(!empty($list)){
            //设置导出数据
            $export_title = array(
                "card_iccid"=>"ICCID",
                "card_number"=>"接入号码",
                "create_date"=>"激活日期",
                "status_name"=>"UIM卡状态",
            );

            $export_list = array(
                "excel_title"=>$file_name,
                "export_title"=>$export_title,
                "export_data"=>$list,
            );

            $this->all_save_excel($export_list);
            return true;
        }else{
            return false;
        }

    }

    /**
     * 商户卡流量查询 全部下载
     */
    function all_card_merchant($download,$file_name){
        global $download_remark;
        $is_success  = true;
        $s_number = $download["download_cardlist"];
        $time_arr = explode("-",date("Y-m",$download['download_starttime']));
        $where = array("flow_record_year"=>$time_arr[0],"flow_record_month"=>$time_arr[1]);
        if(!empty($s_number)) {
            $_GET['s_number'] = $s_number;
            $where["r.card_iccid"] = array("IN",explode(",",$s_number));
        }


        $list = $this->m_flow_record->alias("r")->field("u.user_nickname,c.card_number,c.card_iccid,r.package_data_month,r.package_data_add")->join("cmf_wl_card c ON c.card_number = r.card_number","INNER")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)->select();
        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];
        foreach($list AS &$nitem){
            $nitem["use_flow"] = 0;
            sleep($this->sleep_time);
            $card_use = $telecom->getPoolMemQry(1,$nitem["card_number"]);
            if(isset($card_use['PoolInfo'])){
                $card_use['PoolInfo']['pool_left'] = $card_use['pool_left'];
//                $use_flow = $card_use['PoolInfo']["POOL_MEMBER_ALREADY"];
//                $nitem["use_flow"] = $use_flow;
                if($download["proportion"] != 1 ){   //代理商
                    $plus_flow = kbToM($card_use["PoolInfo"]["POOL_MEMBER_TOTAL"]-$card_use["PoolInfo"]["POOL_MEMBER_ALREADY"])*$this->wl_proportion;
                    $nitem["package_data_month"] = ($nitem["package_data_month"])."M";
                    $nitem["package_data_add"] = ($nitem["package_data_add"])."M";
                }else{  //实际
                    $plus_flow = kbToM($card_use["PoolInfo"]["POOL_MEMBER_TOTAL"]-$card_use["PoolInfo"]["POOL_MEMBER_ALREADY"]);
                    $nitem["package_data_month"] = ($nitem["package_data_month"]/$this->wl_proportion)."M";
                    $nitem["package_data_add"] = ($nitem["package_data_add"]/$this->wl_proportion)."M";
                }

                $nitem["plus_flow"] = $plus_flow."M";

            }else{
                $nitem["card_number"] = "运营商接口请求失败";
                $nitem["plus_flow"] = '';
                $nitem["package_data_month"] = '';
                $nitem["package_data_add"] = '';
                $download_remark = $this->api_fail;
                continue;
            }

        }


        if(!empty($list)){
            //设置导出数据
            $export_title = array(
//                "user_nickname"=>"所属用户",
                "card_iccid"=>"ICCID",
                "card_number"=>"卡号",
                "plus_flow"=>"当月剩余流量",
                "package_data_month"=>"当月月套餐",
                "package_data_add"=>"当月增量包",
            );

            $export_list = array(
                "excel_title"=>$file_name,
                "export_title"=>$export_title,
                "export_data"=>$list,
            );
            $this->all_save_excel($export_list);
            return true;
        }else{
            return false;
        }

    }

    /**
     * 流量池成员使用量 全部下载
     */
    function all_flow_month($download,$file_name){
        global $download_remark;
        $is_success = true;
        $download_remark = $file_name." 下载成功!";
        $list = array();
        $telecom = new \Wulian\Lib\telecom();
        $telecom->poolNbr = $this->wl_config['flow_pool_id'];

        $s_date = $download["download_date_type"];

        if(!empty($s_date)){
            $s_date = $s_date==2?0:$s_date;

            $s_number = $download["download_cardlist"];
            $where = array();
            if(!empty($s_number)) {
                $where = array("card_iccid"=>$s_number);
            }
            $card = $this->m_card->where($where)->select();
            foreach($card AS $citem){
                sleep($this->sleep_time);
                $card_use = $telecom->getPoolMemQry($s_date,$citem['card_number']);
                if(isset($card_use['PoolInfo'])){
                    $card_use['PoolInfo']['pool_left'] = $card_use['pool_left'];
                    $card_use["PoolInfo"]["card_iccid"] = $citem["card_iccid"];
                    $list[] = $card_use['PoolInfo'];
                }else{
                    $temp = array();
                    $temp["card_iccid"] = $citem["card_iccid"];
                    $temp["ACC_NBR"] = "运营商接口请求失败";
                    $temp["return_code"] = -1;
                    $list[] = $temp;
                    $download_remark = $this->api_fail;
                    continue;
                }
            }


            foreach($list AS &$litem){
                if(!isset($litem["return_code"])){
                    $litem['POOL_MEMBER_TOTAL'] = kbToM($litem['POOL_MEMBER_TOTAL'])."M";
                    $litem['POOL_MEMBER_ALREADY'] = kbToM($litem['POOL_MEMBER_ALREADY'])."M";
                    $litem['pool_left'] = kbToM($litem['pool_left'])."M";

                    if($s_date == 0){   //上月
                        $all_flow = getPreFlowByCardNumber($litem['ACC_NBR']);
                    }else{
                        $all_flow = getFlowByCardNumber($litem['ACC_NBR']);
                    }
                    if($download["proportion"] == 1){   //实际版本
                        $litem["POOL_MEMBER_TOTAL"] = ($all_flow/$this->wl_proportion)."M";
                        $litem["POOL_MEMBER_ALREADY"] =  kbToM($litem["POOL_MEMBER_ALREADY"]*$this->wl_proportion)."M";
                    }else{
                        $litem["POOL_MEMBER_TOTAL"] = ($all_flow)."M";
                        $litem['POOL_MEMBER_ALREADY'] = kbToM($litem['POOL_MEMBER_ALREADY'])."M";
                    }

                }else{
                    $litem["POOL_MEMBER_TOTAL"] = "";
                    $litem["POOL_MEMBER_ALREADY"] = "";
                    $litem["pool_left"] = "";
                }

            }
            $this->assign("list",$list);

        }else{
            $download_remark = "日期获取失败";
            $is_success = false;
        }


        if($list && $is_success){
            //设置导出数据
            $export_title = array(
                "card_iccid"=>"ICCID",
                "ACC_NBR"=>"成员号码",
                "POOL_MEMBER_TOTAL"=>"成员分配额度",
                "POOL_MEMBER_ALREADY"=>"成员支出量值",
                "pool_left"=>"池剩余量",
            );

            $export_list = array(
                "excel_title"=>$file_name,
                "export_title"=>$export_title,
                "export_data"=>$list,
            );
            $this->all_save_excel($export_list);
            return true;
        }else{
            return false;
        }



    }

    /**
     * 卡历史导出 全部下载
     */
    function all_import_history($download_id,$file_name){
        global $download_remark;

        $download = $this->m_download->where(array("download_id"=>$download_id))->find();

        $str_start_time = $download["download_starttime"];
        $str_end_time = $download["download_endtime"];
        $s_number = $download["download_cardlist"];

        $where = array(
            "create_time"=>array("between",array($str_start_time,$str_end_time))
        );
        if(!empty($s_number)){
            $where["card_iccid"] = array("like","%".$s_number."%");
        }

        $card_import_list = $this->m_card_import->where($where)
            ->select();

        foreach($card_import_list AS &$citem){
            $citem["create_time"] = date("Y-m-d H:i:s");
            if($citem["card_import_status"] == 1){
                $citem["card_import_status"] = "导入成功";
            }else{
                $citem["card_import_status"] = "导入失败";
            }
        }

        //设置导出数据
        $export_title = array(
            "card_number"=>"卡号",
            "card_iccid"=>"iccid",
            "create_time"=>"导入时间",
            "card_import_status"=>"激活状态",
        );

        $export_list = array(
            "excel_title"=>$file_name,
            "export_title"=>$export_title,
            "export_data"=>$card_import_list,
        );
        $this->all_save_excel($export_list);
        return true;

    }

    /**
     * 卡激活历史
     */
    function all_active_history($download_id,$file_name){
        $download = $this->m_download->where(array("download_id"=>$download_id))->find();

        $str_start_time = $download["download_starttime"];
        $str_end_time = $download["download_endtime"];
        $s_number = $download["download_cardlist"];


        $where = array(
            "create_time"=>array("between",array($str_start_time,$str_end_time))
        );
        if(!empty($s_number)){
            $where["card_iccid"] = array("like","%".$s_number."%");
        }

        $card_active_list = $this->m_card_activate->where($where)->select();

        foreach($card_active_list AS &$citem){
            $citem["create_time"] = date("Y-m-d H:i:s",$citem["create_time"]);
            if($citem["card_activate_status"] == 1){
                $citem["card_activate_status"] = "成功收到激活消息";
            }else{
                $citem["card_activate_status"] = "激活失败";
            }
        }

        if($card_active_list){
            //设置导出数据
            $export_title = array(
                "card_iccid"=>"ICCID",
                "card_number"=>"接入号码",
                "create_time"=>"激活发起时间",
                "card_activate_status"=>"激活状态",
            );

            $export_list = array(
                "excel_title"=>$file_name,
                "export_title"=>$export_title,
                "export_data"=>$card_active_list,
            );
            $this->all_save_excel($export_list);
            return true;
        }else{
            return false;
        }

    }

    /**
     * 停机保号
     */
    function all_card_disabled($download,$file_name){
        global $download_remark;
        $is_success = true;
        $list = array();
        $s_number = $download["download_cardlist"];
        $where = array();
        if (!empty($s_number)) {
            $number_list = explode(",", $s_number);
            $where["card_iccid"] = array("IN",$number_list);
        }

        $card_list = $this->m_card->where($where)->select();


        if(!empty($card_list)){
            $telecom = new \Wulian\Lib\telecom();
            foreach ($card_list AS $nitem) {
                sleep($this->sleep_time);
                $temp = array();
                $data = $telecom->getQueryCardStatus($nitem["card_number"]);
                $temp["card_iccid"] = $nitem["card_iccid"];
                if(isset($data['Query_response']) && !empty($data['Query_response'])){
                    $temp['card_number'] = $data['Query_response']['number'];
                    $temp['status_name'] = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['productStatusName'];
                    $temp['status_code'] = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['productStatusCd'];
                    if (!is_array($data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'])) {
                        $create_date = $data['Query_response']['prodRecords']['prodRecord']['productInfo']['servCreateDate'];
                        $create_date = substr($create_date, 0, 4) . "-" . substr($create_date, 4, 2) . "-" . substr($create_date, 6, 2);
                        $temp['create_date'] = $create_date;
                    } else {
                        $temp['create_date'] = "--";
                    }
                    $list[] = $temp;
                }else{
                    $temp = array();
                    $temp["card_iccid"] = $nitem["card_iccid"];
                    $temp["card_number"] = "运营商接口请求失败";
                    $temp["create_date"] = "";
                    $temp["status_name"] = "";
                    $list[] = $temp;
                    $download_remark = $this->api_fail;
                    continue;
                }
            }
        }


        if(!empty($list) && $is_success){
            //设置导出数据
            $export_title = array(
                "card_iccid" => "ICCID",
                "card_number" => "接入卡号",
                "create_date" => "激活日期",
                "status_name" => "产品状态",
            );

            $export_list = array(
                "excel_title" => $file_name,
                "export_title" => $export_title,
                "export_data" => $list,
            );
            $this->all_save_excel($export_list);

            return true;
        }else{
            return false;
        }


    }

    /**
     * 停复机历史 导出
     */
    function all_card_disabled_history($download,$filename){

        $s_start_time = $download["download_starttime"];
        $s_end_time = $download["download_endtime"];
        $s_number = $download["download_cardlist"];


        $where = array(
            "create_time"=>array("between",array($s_start_time,$s_end_time))
        );
        if(!empty($s_number)){
            $where["card_iccid"] = array("like","%".$s_number."%");
        }

        $card_status_list = $this->m_card_status->where($where)->select();

        foreach($card_status_list AS &$citem){
            $citem["create_time"] = date("Y-m-d H:i:s",$citem["create_time"]);
            if($citem["card_status_status"] == 1){
                $citem["card_status_status"] = "服务器成功收到请求消息";
            }else{
                $citem["card_status_status"] = "请求失败";
            }
        }

        if(!empty($card_status_list)){
            //设置导出数据
            $export_title = array(
                "card_iccid" => "ICCID",
                "card_number" => "接入卡号",
                "create_time" => "停复机时间",
                "card_status_status" => "停复机状态",
            );

            $export_list = array(
                "excel_title" => $filename,
                "export_title" => $export_title,
                "export_data" => $card_status_list,
            );
            $this->all_save_excel($export_list);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 订单列表 导出
     */
    function all_order($download,$filename){

        $s_start_time = $download["download_starttime"];
        $s_end_time = $download["download_endtime"];
        $s_number = $download["download_cardlist"];
        $s_uid = $download["download_uid"];

        $where = array();
//        $where = array(
//            "o.create_time"=>array("between",array($s_start_time,$s_end_time))
//        );
        if(!empty($s_number)){
            $where["o.card_iccid"] = $s_number;
        }
        if(!empty($s_uid)){
            $where["c.uid"] = $s_uid;
        }
        $where["o.order_status"] = 1;


        $order_list = $this->m_order->alias("o")->field("o.order_number,o.package_type,o.card_number,o.card_iccid,o.package_data,o.order_price,o.order_source,o.create_time,u.user_nickname")->join("cmf_wl_card c ON c.card_number = o.card_number","LEFT")->join("cmf_users u ON u.id = c.uid","LEFT")->where($where)->select();
        foreach($order_list AS &$oitem){
            if($oitem["package_type"] == 1){
                $oitem["package_type"] = "月套餐";
            }elseif($oitem["package_type"] == 2){
                $oitem["package_type"] = "增量包";
            }elseif($oitem["package_type"] == 3){
                $oitem["package_type"] = "年套餐续费";
            }

            $oitem["create_time"] = date("Y-m-d H:i:s",$oitem["create_time"]);

            if($oitem["order_source"] == 1){
                $oitem["order_source"] = "管理员操作";
            }elseif($oitem["order_source"] == 2){
                $oitem["order_source"] = "用户购买";
            }
            if($download["proportion"]){    //实际
                $oitem["package_data"] =  ($oitem["package_data"]/$this->wl_proportion)."M";
            }else{
                $oitem["package_data"] =  $oitem["package_data"]."M";
            }
        }



        if(!empty($order_list)){
            //设置导出数据
            $export_title = array(
                "user_nickname"=>"卡所属人",
                "order_number"=>"订单号",
                "package_type"=>"类型",
                "card_number"=>"卡号",
                "card_iccid"=>"iccid",
                "package_data"=>"流量值",
                "order_price"=>"价格",
                "order_source"=>"来源",
                "create_time"=>"创建时间",
            );

            $export_list = array(
                "excel_title"=>$filename,
                "export_title"=>$export_title,
                "export_data"=>$order_list,
            );
            $this->all_save_excel($export_list);
            return true;
        }else{
            return false;
        }
    }


    /**
     * 定时任务 执行下载
     */
    function _cron_download($type,$download_id){
        global $download_remark;
        $download_status = 2;
        $download = $this->m_download->where(array("download_id"=>$download_id))->find();
        $this->m_download->where(array("download_id"=>$download_id))->save(array("begin_time"=>date("Y-m-d H:i:s"),"download_status"=>1));
        if($type == "flow_member"){ //流量池成员下载
            $file_name = "流量池成员-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."下载成功！";
            $result = $this->all_flow_member($download,$file_name);

        }elseif($type=='flow_month'){
            $file_name = "卡流量使用量-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."下载成功！";
            $result = $this->all_flow_month($download,$file_name);
        }elseif($type == 'card_status'){    //卡状态查询
            $file_name = "UIM卡状态-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."下载成功！";
            $result = $this->all_card_status($download,$file_name);

        }elseif($type=="card_merchant"){    //商户卡流量查询
            $file_name = "商户卡流量-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."下载成功！";
            $result = $this->all_card_merchant($download,$file_name);
        }elseif($type == 'import_history'){ //卡历史导出
            $file_name = "卡导入历史-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."-".date("Y-m-d",$download["download_starttime"])."至".date("Y-m-d",$download["download_endtime"])."下载成功！";
            $result = $this->all_import_history($download_id,$file_name);
        }elseif($type == 'active_history'){
            $file_name = "激活历史-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."-".date("Y-m-d",$download["download_starttime"])."至".date("Y-m-d",$download["download_endtime"])."下载成功！";
            $result = $this->all_active_history($download_id,$file_name);
        }elseif($type == 'card_disabled'){
            $file_name = "停机保号管理-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."下载成功！";
            $result = $this->all_card_disabled($download,$file_name);
        }elseif($type == 'card_disabled_history'){
            $file_name = "停复机历史-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."下载成功！";
            $result = $this->all_card_disabled_history($download,$file_name);
        }elseif($type == 'order'){
            $file_name = "订单列表-".date("YmdHis").rand(100,999);
            $download_remark = $file_name."下载成功！";
            $result = $this->all_order($download,$file_name);
        }


        if(isset($result) && $result){
             $this->m_download->where(array("download_id"=>$download_id))->save(array("download_remark"=>$download_remark,"download_status"=>2,"download_down_url"=>"data/download/".$file_name.".xls","download_filename"=>$file_name.".xls","finish_time"=>date("Y-m-d H:i:s",time()),));
        }else{
            $this->m_download->where(array("download_id"=>$download_id))->save(array("download_remark"=>"数据为空","finish_time"=>date("Y-m-d H:i:s",time()),"download_status"=>3));
        }

    }

    /**
     * 下载
     * 执行定时任务
     */
    function cron(){
        $down_list = $this->m_download->where("is_del = 0 AND download_status =0")->select();
        foreach($down_list AS $ditem){
            echo "ID:".$ditem["download_id"]." 正在处理";
            $this->_cron_download($ditem["download_type"],$ditem["download_id"]);
            echo "处理结束</br>";
        }
        echo "success";
    }
}